''微信公众号关注：VB小源码
''QQ群：344402874
''博客：https://www.cnblogs.com/vbee/
''个人微信：vbee_club

Imports System.IO
Imports System.Management
Imports System.Security.Cryptography
Imports System.Text

''' <summary>
''' DES加密解密类
''' </summary>
Public Class Cls_des
  ''' <summary>
  ''' 密码
  ''' </summary>
  Private pass_key As String = "00000000"

  ''' <summary>
  ''' 初始化
  ''' </summary>
  ''' <param name="key"></param>
  Public Sub New(Optional key As String = "r9j8w5ed")
    pass_key = key
  End Sub

  ''' <summary>
  ''' DES加密
  ''' </summary>
  ''' <param name="str_s"></param>
  ''' <returns></returns>
  Public Function DesEncrypt(str_s As String) As String
    Dim byts As Byte() = Encoding.UTF8.GetBytes(pass_key.Substring(0, 8))
    Dim IV As Byte() = byts
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(str_s)
    Dim descPro As DESCryptoServiceProvider = New DESCryptoServiceProvider()
    Dim mStr As MemoryStream = New MemoryStream()
    Dim cStream As CryptoStream = New CryptoStream(mStr, descPro.CreateEncryptor(byts, IV), CryptoStreamMode.Write)
    cStream.Write(bytes, 0, bytes.Length)
    cStream.FlushFinalBlock()
    Return Convert.ToBase64String(mStr.ToArray())
  End Function

  ''' <summary>
  ''' des解密
  ''' </summary>
  ''' <param name="dstr"></param>
  ''' <returns></returns>
  Public Function DesDecrypt(dstr As String) As String
    Dim byts As Byte() = Encoding.UTF8.GetBytes(pass_key.Substring(0, 8))
    Dim IV As Byte() = byts
    Dim arr As Byte() = Convert.FromBase64String(dstr)
    Dim dSerPro As DESCryptoServiceProvider = New DESCryptoServiceProvider()
    Dim mStream As MemoryStream = New MemoryStream()
    Dim cStream As CryptoStream = New CryptoStream(mStream, dSerPro.CreateDecryptor(byts, IV), CryptoStreamMode.Write)
    cStream.Write(arr, 0, arr.Length)
    cStream.FlushFinalBlock()
    Return Encoding.UTF8.GetString(mStream.ToArray())
  End Function


End Class

''' <summary>
''' 获取机器码
''' </summary>
Public Class Cls_MachineCode

  ''' <summary>
  ''' 获取CPU_ID
  ''' </summary>
  ''' <returns></returns>
  Public Function GetCpuInfo() As String
    Dim txt As String = " "
    Using mClass As ManagementClass = New ManagementClass("Win32_Processor")
      Dim insts As ManagementObjectCollection = mClass.GetInstances()
      For Each mObject As ManagementBaseObject In insts
        Dim maObject As ManagementObject = CType(mObject, ManagementObject)
        txt = maObject.Properties("ProcessorId").Value.ToString()
        maObject.Dispose()
      Next
    End Using
    Return txt.ToString()
  End Function

  ''' <summary>
  ''' 获取硬盘ID
  ''' </summary>
  ''' <returns></returns>
  Public Function GetHDid() As String
    Dim txt As String = " "
    Using mClass As ManagementClass = New ManagementClass("Win32_DiskDrive")
      Dim insts As ManagementObjectCollection = mClass.GetInstances()
      For Each mBaseObject As ManagementBaseObject In insts
        Dim mObject As ManagementObject = CType(mBaseObject, ManagementObject)
        txt = CStr(mObject.Properties("Model").Value)
        mObject.Dispose()
      Next
    End Using
    Return txt.ToString()
  End Function

  ''' <summary>
  ''' 获取MAC地址
  ''' </summary>
  ''' <returns></returns>
  Public Function GetMoAddress() As String
    Dim txt As String = " "
    Using mClass As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
      Dim insts As ManagementObjectCollection = mClass.GetInstances()
      For Each mBaseObject As ManagementBaseObject In insts
        Dim mObject As ManagementObject = CType(mBaseObject, ManagementObject)
        Dim fg As Boolean = mObject("IPEnabled")
        If fg Then
          txt = mObject("MacAddress").ToString()
        End If
        mObject.Dispose()
      Next
    End Using
    Return txt.ToString()
  End Function

  ''' <summary>
  ''' 生成机器码
  ''' </summary>
  ''' <returns></returns>
  Public Function GenerateMachineCode() As String
    Dim des As New Cls_des
    Dim cpu_id As String = GetCpuInfo()
    Dim text As String = des.DesEncrypt(cpu_id & GetHDid() & GetMoAddress())
    Dim text2 As String = text.Substring(0, 25)
    text2 = text2.ToUpper()
    Return String.Concat(New String() {text2.Substring(0, 5), "-", text2.Substring(5, 5), "-", text2.Substring(10, 5), "-", text2.Substring(15, 5), "-", text2.Substring(20, 5)})
  End Function
End Class
